
package com.longway.common.kit;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfplugin.mail.MailKit;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;

/**
 * 邮件发送工具类
 */
public class EmailKit {

    private static final Log log = Log.getLog(EmailKit.class);

    public static String sendEmail(String fromEmail, String toEmail, String title, String content) {
        return sendEmail(null, fromEmail, null, toEmail, title, content);
    }


    /**
     * 测试邮件发送
     * 访问地址：/mailTest
     */
    public static void mailTest() {
        MailKit.use("mail1").send("13935062754@163.com",
                Collections.emptyList(), "代开电子发票",
                "您的代开发票已添加至附件，请下载查收！",
                Arrays.asList(new File("D:\\Users\\mac\\ryc\\upload\\xxb\\upload\\fp\\5000\\20230713105033778.pdf")));
        System.out.println("成功");
    }


    /**
     * 邮件发送
     *
     * @param toEmail  收件邮箱
     * @param title    邮箱标题
     * @param filePath 文件路径
     */
    public static void mailSend(String toEmail, String title, String filePath) {
        MailKit.use("mail1").send(toEmail, Collections.emptyList(), title,
                "您的代开发票已添加至附件，请下载查收！", Collections.singletonList(new File(filePath)));
        System.out.println("邮寄成功");
    }


    public static String sendEmail(String emailServer, String fromEmail, String password, String toEmail, String title, String content) {

        SimpleEmail email = new SimpleEmail();
        if (StrKit.notBlank(emailServer)) {
            email.setHostName(emailServer);
        } else {
            // 默认使用本地 postfix 发送，这样就可以将postfix 的 mynetworks 配置为 127.0.0.1 或 127.0.0.0/8 了
            email.setHostName("127.0.0.1");
        }

        // 如果密码为空，则不进行认证
        if (StrKit.notBlank(password)) {
            email.setAuthentication(fromEmail, password);
        }

        email.setCharset("utf-8");
        try {
            email.addTo(toEmail);
            email.setFrom(fromEmail);
            email.setSubject(title);
            email.setMsg(content);
            return email.send();
        } catch (EmailException e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        mailTest();
//		String ret = sendEmail(
//				"abc.com",              // 邮件发送服务器地址
//				"no-reply@abc.com",		// 发件邮箱
//				null,					// 发件邮箱密码
//				"test@test.com",		// 收件地址
//				"邮件标题",              // 邮件标题
//				"content");				// 邮件内容
//		System.out.println("发送返回值: " + ret);
    }
}
		
		
	
	


